package service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import model.Tgood;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import dao.CashierDaoI;
import dao.GoodDaoI;
import pageModel.DataGrid;
import pageModel.Good;
import service.CashierServiceI;
@Service("cashierService")
public class CashierServiceImpl implements CashierServiceI{
private CashierDaoI cashierDao;
private GoodDaoI goodDao;
	public CashierDaoI getCashierDao() {
	return cashierDao;
}
@Autowired
public void setCashierDao(CashierDaoI cashierDao) {
	this.cashierDao = cashierDao;
}
public GoodDaoI getGoodDao() {
	return goodDao;
}
@Autowired
public void setGoodDao(GoodDaoI goodDao) {
	this.goodDao = goodDao;
}
	@Override
	public DataGrid show(Good good) {
		DataGrid dg=new DataGrid();
		String hql="from Tgood t where t.code=:code";
		Map<String,Object> params=new HashMap<String,Object>();
		/*System.out.println("***"+good.getFoodid());*/
		params.put("code", good.getFoodid());
		String totalHql="select count(*) "+hql;
		if(good.getSort()!=null){
		hql+=" order by "+good.getSort()+" "+good.getOrder();
		}
		List<Tgood> su=goodDao.find(hql,params,good.getPage(),good.getRows());
		List<Good> l=new ArrayList<Good>();
		if(su!=null&&su.size()>0){
			for(Tgood t :su){
				Good u=new Good();
				BeanUtils.copyProperties(t, u);
				u.setAmount(1);
				u.setNprice(u.getAmount()*u.getPrice());
				l.add(u);
			}
		}
		dg.setTotal(goodDao.count(totalHql,params));
		dg.setRows(l);
		return dg;
	}
}
